package hello;

import java.util.*;
import java.math.*;

import org.dom4j.*;
import org.dom4j.io.*;

import org.hibernate.*;
import persistence.*;
import auction.model.*;

public class HelloWorldManyToManyMap
{
	
	
	public static void main(String[] args)throws Exception
	{
		
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();

		
		CategoryManyToManyMap catA =new CategoryManyToManyMap("cat a "+new Date());
		ItemManyToManyMap iA=new ItemManyToManyMap("item a "+new Date());
		UserManyToManyMap uA=new UserManyToManyMap("wei");
		
		Map<ItemManyToManyMap,UserManyToManyMap> itemsAndUser=new HashMap<ItemManyToManyMap,UserManyToManyMap>();
		
		itemsAndUser.put(iA, uA);
		catA.setItemsAndUser(itemsAndUser);
		
		s.save(catA);
		s.save(iA);
		s.save(uA);

		
		tx.commit();
		s.close();
		
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		
		
		CategoryManyToManyMap loadedCat=(CategoryManyToManyMap)s1.load(CategoryManyToManyMap.class,1L);
		
		ItemManyToManyMap loadedItem=(ItemManyToManyMap)s1.load(ItemManyToManyMap.class,1L);
		
		//removed from map by map-key
		loadedCat.getItemsAndUser().remove(loadedItem);
		
		//UserManyToManyMap loadedUser=(UserManyToManyMap)s1.load(UserManyToManyMap.class,2L);
		//loadedCat.getItemsAndUser().put(loadedItem, loadedUser);
		
		out("#####"+loadedCat.getItemsAndUser());
		
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println(obj);
	}
}
